package io.adminboot.service.impl;

import io.adminboot.entity.SysUserRole;
import io.adminboot.repository.extend.SysUserRoleExtMapper;
import io.adminboot.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



/**
 * 用户与角色对应关系
 * 
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
	
	@Autowired
	private SysUserRoleExtMapper mapper;

	@Override
	public void saveOrUpdate(Long userId, List<Long> roleIdList) {
		//先删除用户与角色关系
		mapper.deleteByUserId(userId);

		if(roleIdList == null || roleIdList.size() == 0){
			return ;
		}

		//保存用户与角色关系
		for(Long roleId : roleIdList){
			SysUserRole entity = new SysUserRole();
			entity.setUserId(userId);
			entity.setRoleId(roleId);

			mapper.insert(entity);
		}
	}

	@Override
	public List<Long> queryRoleIdList(Long userId) {
		return mapper.selectRoleIdList(userId);
	}

	@Override
	public int deleteByRoleIds(Long[] roleIds){
		return mapper.deleteByRoleIds(roleIds);
	}
}
